<?php
// src/Pyaf/GymBundle/Controller/AdminRutinaController.php
namespace Pyaf\GymBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Pyaf\GymBundle\Entity\Rutina;
use Pyaf\GymBundle\Form\RutinaType;

/**
 * Description of AdminRutinaController
 *
 * @author Toshiba P755-S5272
 */
class AdminRutinaController extends Controller{
    
    public function listAction(){
        $rutinas = $this->getDoctrine()->getEntityManager()
                        ->getRepository('PyafGymBundle:Rutina')->findAllOrderByNombre();
        return $this->render('PyafGymBundle:AdminRutina:list.html.twig', array('rutinas' => $rutinas));
    }
    
    public function newAction() {
        
        $peticion = $this->getRequest();
                
        $rutina = new Rutina();     
        $formulario = $this->createForm(new RutinaType(), $rutina);
        
        if ($peticion->getMethod() == 'POST') {
            $formulario->bindRequest($peticion);
            if ($formulario->isValid()) {
                $em = $this->getDoctrine()->getEntityManager();
                $rutina = $formulario->getData();            
                $em->persist($rutina);
                $em->flush();
                
                $this->get('session')
                         ->setFlash(
                                        'alertaRutinaSuccess',
                                        'La nueva rutina ('.$rutina->getNombre().') ha sido agregada correctamente!'
                                    );
                
                return $this->redirect($this->generateUrl('admin_rutina_list'));
            } 
        }
        
        return $this->render('PyafGymBundle:AdminRutina:new.html.twig', array(
            'formulario' => $formulario->createView(),
        ));
    }
    
    public function editAction($id)
    {
        $peticion = $this->get('request');
        $em = $this->getDoctrine()->getEntityManager();

        if (null == $rutina = $em->find('PyafGymBundle:Rutina', $id)) {
            throw new NotFoundHttpException('No existe la rutina que se quiere modificar');
        }

        $formulario = $this->createForm(new RutinaType(), $rutina);
        $formulario->setData($rutina);

        if ($peticion->getMethod() == 'POST') {
            $formulario->bindRequest($peticion);

            if ($formulario->isValid()) {
                $em->persist($rutina);
                $em->flush();
                
                $this->get('session')
                         ->setFlash(
                                        'alertaRutinaSuccess',
                                        'La rutina ('.$rutina->getNombre().') ha sido modificada correctamente!'
                                    );

                return $this->redirect($this->generateUrl('admin_rutina_list'));
            }
        }

        return $this->render('PyafGymBundle:AdminRutina:edit.html.twig', array(
            'formulario' => $formulario->createView(),
            'rutina'   => $rutina
        ));
    }
    
    public function disableAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();
        $rutina = $em->find('PyafGymBundle:Rutina', $id);
        if (null == $rutina) {
            throw new NotFoundHttpException('No existe la rutina que se quiere modificar');
        } else {  
            $rutina->setHabilitado(!$rutina->getHabilitado());
            $em->getRepository('PyafGymBundle:Rutina')->save($rutina);
            
            if($rutina->getHabilitado()){
                $this->get('session')
                         ->setFlash(
                                        'alertaRutinaSuccess',
                                        'La rutina ('.$rutina->getNombre().') ha sido habilitada correctamente!'
                                    );
            } else{
                $this->get('session')
                         ->setFlash(
                                        'alertaRutinaError',
                                        'La rutina ('.$rutina->getNombre().') ha sido inhabilitada correctamente!'
                                    );
            }
        }       

        return $this->redirect($this->generateUrl('admin_rutina_list'));
    }
    
    public function showAction($id){        
        $rutina = new Rutina(); 
        $rutina = $this->getDoctrine()->getEntityManager()
                         ->getRepository('PyafGymBundle:Rutina')
                         ->findRutina($id);
              
        return $this->render('PyafGymBundle:AdminRutina:info.html.twig', array('rutina' => $rutina));
    }
}

?>
